/*
* @Author: ZHELIHAO\fangshuqin
* @Date:   2018-05-07 20:00:23
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-26 19:14:35
*/
{
    let EVENT_CLICK = Laya.Event.CLICK;

    
    class Rolesingle extends ui.Role.SingleUI {
        constructor () {
            super();
            
            this.init();
        }

        init () {
            this.initEvent();
            this.initConfig();
            this.registerActions();
            this.bar.mediator = new Com.Role.RoleBarMediator(this);
        }

                // 初始化配置
        initConfig() {
            this.config = {
                MAX_WIDTH: 212,
                MIN_WIDTH: 10

            }
        }
   
        registerActions() {
            console.log("single 注册");
            this.ACTIONS = {
                 "getUserOneHeroInfo"  : this.getSingleHeroInfo, //单个英雄
                 [GAME_CMDS.UPBYITEM]  : this.getSingleHeroInfo  //升级
            };
            Sail.io.register(this.ACTIONS, this);
        }

        getSingleHeroInfo(data, code,msg){
            this.bar.mediator.setData(data);
            this.dom_des.text = data.weapon.desc;
            if( this.roleNormal){
                this.roleNormal.destroy();
            }

            if(this.roleAni){
               this.roleAni.destroy(); 
            }

            if ( data.no == 1 || data.no == 2 ){
                let aniPath = `assets/role/${data.no}/other/skin_1`;
                Laya.loader.load([{url : `${aniPath}.png`, type : Laya.Loader.IMAGE}, {url : `${aniPath}.sk`, type : Laya.Loader.BUFFER}], Laya.Handler.create(this, function () {
                    let roleAni = this.roleAni = Sail.Utils.createSkeleton(aniPath);
                        roleAni.pos(175, 330);
                        roleAni.play("enter", false);
                    this.roleBox.addChild(roleAni);

                    roleAni.once(Laya.Event.STOPPED, this, ()=>{
                        roleAni.play("static", true);

                    })
                }));
            }else{
                let roleNormal = this.roleNormal =  new Laya.Image(`res/role/role_big_${data.no}.png`);
                roleNormal.pos(0,0);
                this.roleBox.addChild(roleNormal);
            }
           
            if(data.had == 0){
                this.role_check_button.index = 2;
            }else{
                this.role_check_button.index = 0;
            }
        }

        initEvent(){
            this.role_check_button.on(EVENT_CLICK, this, ()=>{
                let buttonIdx = this.role_check_button.index;
                if(buttonIdx ==0 || buttonIdx ==1){
                    console.log("使用中 or 更换");
                }else if(buttonIdx ==2){
                    console.log("前往获取");
                }
            })
        }

        exit () {
            Sail.io.unregister(this.ACTIONS);
        }

        resize (width, height, rate) {
            if(rate < 1){
                this.scale(rate, rate);
            }else{
                this.scale(1, 1);
            }
        }
    }

    Sail.class(Rolesingle, "Com.Role.Rolesingle");
}